package com.oraycn.esbasic.helpers;

import java.lang.reflect.Field;

public class ReflectionHelper {

    /**
     * 不同实体类，相同的属性赋值
     *
     * @param sourceData
     * @param toData
     * @param <>
     */
    public static void copyFields(Object sourceData, Object toData) {
        if (sourceData == null || toData == null)
            return;
        Field[] fields = sourceData.getClass().getDeclaredFields();
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                Object value = field.get(sourceData);
                if (null != value && !"".equals(value)) {
                    Field toField = toData.getClass().getDeclaredField(field.getName());
                    if (toField != null) {
                        toField.setAccessible(true);
                        toField.set(toData, value);
                    }
                }
            }
        } catch (Exception ignored) {
        }
    }
} 
